<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$GLOBALS['OUTPUT']->master_section = 'settings';
	$GLOBALS['OUTPUT']->minor_section = $GLOBALS['LANG']->lang_admin->managesmilies;
	$GLOBALS['OUTPUT']->cpmenu = $menus[$GLOBALS['OUTPUT']->master_section];
	
	// TODO: Make sure this auto installs to clients correctly without them having to restart browser
	
	if(isset($_FILES['uloadsmilies'])){
		if(!is_writable("./smilies")){
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->smileydirectorynotwritable;
		}else{
			preg_match("#\.(png|jpg|gif|zip|jpeg)$#",$_FILES['uloadsmilies']['name'],$match);
			if($match){
				if($match[1] != 'zip'){
					if(move_uploaded_file($_FILES['uloadsmilies']['tmp_name'],"./smilies/".microtime_float().".".$match[1])){
						$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->imageuploadcomplete;
					}else
						$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->invalidsmileyuploadfiletype;
				}else{
					// Handle zip upload
					if(($error = lib_zip::extract($_FILES['uloadsmilies']['tmp_name'],"./smilies",true,"#\.(png|jpg|gif|zip|jpeg)$#")) !== true)
						$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->zipinvalid;
					else
						$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->zipuploadsmileycomplete;
				}
			}else
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->invalidsmileyuploadfiletype;
		}
	}
	
	$add = x7in::read('smiley','a');
	if(!empty($add)){
		foreach($add as $key=>$val){
			if(empty($val)) continue;
			
			// TODO: Check that a # in a smiley or filter does not break it
			$key = base64_decode($key);
			$settings['filter'][preg_quote($val)] = "<img src=\"./smilies/$key\" />";
		}
		$settings['key'] = 'settings';
		$GLOBALS['DATABASE']->put('system',$settings);
		$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->smileyupdated;
	}
	
	$delete_smiley = x7in::read('delete_smiley');
	if(!empty($delete_smiley)){
		$delete_smiley = base64_decode($delete_smiley);
		if(isset($settings['filter'][$delete_smiley])){
			unset($settings['filter'][$delete_smiley]);
			$settings['key'] = 'settings';
			$GLOBALS['DATABASE']->put('system',$settings);
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->smileydel;
		}
	}
	
	$delete_image = x7in::read('delete_file');
	if(!empty($delete_image)){
		$delete_image = base64_decode($delete_image);
		if(is_file("./smilies/$delete_image")){
			unlink("./smilies/$delete_image");
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->imagedel;
		}
	}
	
	$smilies = $settings['filter'];
	foreach($smilies as $key=>$val){
		if($val[0] != '<')
			unset($smilies[$key]);
	}
	
	$unused = array();
	$sdir = dir("./smilies");
	while($file = $sdir->read()){
		if($file[0] == '.') continue;
		
		if(!in_array("<img src=\"./smilies/{$file}\" />",$smilies))
			$unused[] = $file;
	}
	
	$GLOBALS['OUTPUT']->smilies = $smilies;
	$GLOBALS['OUTPUT']->unused = $unused;
	
	echo tpl_managesmilies::managesmilies();
?>